package com.lianzhihui.romeex.widget.dialog;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.inter.DialogListener;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.UiUtils;
import com.lianzhihui.romeex.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 密码验证通用 弹窗
 * Author: RAMON
 * CreateDate: 2020/9/10 10:06 AM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class ExchangePasswordDialog extends BaseDialog {

    @BindView(R.id.tv_title)
    TextView titleTv;
    @BindView(R.id.warning)
    TextView warning;
    @BindView(R.id.input)
    EditText input;
    @BindView(R.id.tv_eyes)
    ImageView tv_eyes;
    @BindView(R.id.bt_confirm)
    Button bt_confirm;
    String address;
    DialogListener listener;
    String title;
    String fromIconImage;
    String toIconImage;
    String fromValue;
    String toValue;
    WalletInfo walletInfo;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.iv_from)
    ImageView ivFrom;
    @BindView(R.id.iv_to)
    ImageView ivTo;
    @BindView(R.id.tv_from)
    TextView tvFrom;
    @BindView(R.id.tv_to)
    TextView tvTo;

    public ExchangePasswordDialog(@NonNull Activity activity) {
        super(activity);
//        this.title = activity.getResources().getString(R.string.input_password);
        this.title = "输入密码";
    }

    public ExchangePasswordDialog(@NonNull Activity activity, String title) {
        super(activity);
        this.title = title;
    }

    public ExchangePasswordDialog(@NonNull Activity activity, String title, String address) {
        super(activity);
        this.title = title;
        this.address = address;
    }

    public void setData(String fromIconImage, String toIconImage, String fromValue, String toValue) {
        this.fromIconImage = fromIconImage;
        this.toIconImage = toIconImage;
        this.fromValue = fromValue;
        this.toValue = toValue;
    }

    public void setTitleCenterHorizontal(int location) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = location;
        titleTv.setLayoutParams(params);
    }

    public void setListener(DialogListener listener) {
        this.listener = listener;
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_exchange_password;
    }

    @Override
    protected void initView() {

        Glide.with(context).load(fromIconImage).into(ivFrom);
        Glide.with(context).load(toIconImage).into(ivTo);
        tvFrom.setText(fromValue);
        tvTo.setText(toValue);
        if (TextUtils.isEmpty(address)) {
            //当前用户
            walletInfo = UserUtils.getCurrentUser();
        } else {
            //地址指定 用户
            walletInfo = UserUtils.getUser(address);
        }

        if (TextUtils.isEmpty(title)) {
        }
        titleTv.setText(title);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                warning.setVisibility(View.INVISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    public void setTitle(String title) {
        titleTv.setText(title);
    }

    @OnClick({R.id.bt_confirm, R.id.tv_eyes, R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:
                String password = input.getText().toString();
                if (walletInfo != null) {
                    String saftyCode = walletInfo.getSaftyCode();
                    if (TextUtils.isEmpty(saftyCode)) {
                        Toast.makeText(context, "用户信息错误", Toast.LENGTH_SHORT).show();
                    } else {
                        if (TextUtils.isEmpty(password)) {
                            warning.setVisibility(View.VISIBLE);
                        } else {
                            if (walletInfo.equSafeCode(password)) {
                                dismiss();
                                listener.onPassed();
                            } else {
                                warning.setVisibility(View.VISIBLE);
                                listener.onFail();
                            }
                        }
                    }
                }

                break;
            case R.id.tv_eyes:
                UiUtils.setTextEyes(tv_eyes, input);
                break;
            case R.id.iv_close:
                dismiss();
                break;
            default:
                break;
        }
    }

}
